/**
 * Created by csy on 2017/11/20.
 */
var $alert = require("libs/alert");
var isSale = require('libs/user-type').isSale;
require(['jquery','libs/confirm','easyui'],function($,$confirm){
    appendOption('api/v1/p/crm/warehouse/query?warehouseType=A',$("#warehouseCode"),"warehouseCode","warehouseName",{warehouseCode:'',warehouseName:'全部'});
    $.parser.parse();
    tableInit();
    //查询按钮点击事件
    $("#main_search").on("click",function () {
        $('#main_table').datagrid('load',queryParams());
    });
    $(document).on("click",'a[name=commit]',function () {
        var id = $(this).data('id');
        ajaxRequestMethod({id: id},"api/v1/p/mms/service/order/consume/commit");
    });
    $(document).on("click",'a[name=delete]',function () {
        var id = $(this).data('id');
        $confirm("确认删除此扣费服务工单？",function () {
            ajaxRequestMethod({id: id},"api/v1/p/mms/service/order/consume/delete");
        });
    });
    $(document).on("click",'a[name=look]',function () {
        window.parent.tabOpen($(this).attr('ref'),"扣费详情",true,true);
    });
});

function tableInit() {
    $("#main_table").datagrid({
        url:'api/v1/p/mms/service/order/consume/list',
        border : false,
        height:$(window).height(),
        pagination : true,
        idField : 'id',
        pageSize : 20,
        pageList : [ 20, 50,  100 ],
        rownumbers:true,
        singleSelect: true,
        emptyMsg : "暂无数据",
        queryParams:queryParams(),
        columns:[[ {
            field : 'code',
            title : '扣费服务编号',
            width:"12%",
            formatter: function(value,row,index){
                return "<a name='look' href='javascript:;' ref='service-detail?id="+row.id+"'>"+value+"</a>";
            }
        },{
            field : 'warehouseName',
            title : '海外仓',
            width:"12%"
        },{
            field : 'entName',
            title : '客户名称',
            width:"15%"
        },{
            field:'relEntityCode',
            title:'关联单号',
            width:"12%"
        },{
            field:'currency',
            title:'币制',
            width:"5%"
        },{
            field:'amount',
            title:'扣费金额',
            width:"6%"
        },{
            field:'factAmount',
            title:'成本金额',
            width:"6%"
        },{
            field:'confirmeStatus',
            title:'确认状态',
            width:"6%",
            formatter:function (value,row,index) {
                switch (value){
                    case "unconfirmed": return "待确认";
                    case "confirmed": return "已确认";
                    case "confirme_not_pass": return "未同意";
                    default : return "";
                }
            },
            styler: function(value,row,index){
                if (value =='confirme_not_pass'){
                    return 'color:red;';
                }
            }
        },{
            field:'auditStatus',
            title:'审核状态',
            width:"6%",
            formatter:function (value,row,index) {
                switch (value){
                    case "unaudited": return "待审核";
                    case "audited": return "已审核";
                    case "audit_not_pass": return "未通过";
                    default : return "";
                }
            },
            styler: function(value,row,index){
                if (value =='audit_not_pass'){
                    return 'color:red;';
                }
            }
        },{
            field:'createdDt',
            title:'创建时间',
            width:"12%"
        },{
            field:'opt',
            title:'操作',
            hidden:isSale,
            width:"10%",
            formatter:function (value,row,index) {
                if (row.processStatus=='open' || row.processStatus=='confirme_not_pass' || row.processStatus == 'audit_not_pass'){
                     return "<a href='javascript:;' name='commit' data-id='"+row.id+"'>提交</a>|<a href='service-add?id="+row.id+"'>修改</a>|<a href='javascript:;' name='delete' data-id='"+row.id+"'>删除</a>";
                }
                return "";
            }
        }
        ]],
        loadFilter:function(data){
            if(data.code=='200'){
                return data.data;
            }else{
                return [];
            }
        },
        toolbar:"#tb"
    });
    $("#tbar").show();
}
function queryParams() {
    var d1 = $('#startDt').datebox('getValue');
    var d2 = $('#endDt').datebox('getValue');
    var data =
        {
            code:$('#code').val(),
            warehouseCode:$('#warehouseCode').combobox('getValue'),
            entName:$('#entName').val(),
            relEntityCode:$('#relEntityCode').val(),
            confirmeStatus:$('#confirmeStatus').combobox('getValue'),
            auditStatus:$('#auditStatus').combobox('getValue'),
            startDate:d1?$('#startDt').data('utc0'):'',
            endDate:d2?$('#endDt').data('utc0'):''
        };
    return data;
}
function ajaxRequestMethod(data,url) {
    $.ajax({
        contentType: "application/json; charset=utf-8",
        data:JSON.stringify(data),
        type:"post",
        url:url,
        success:function (result) {
            if(result.code==200){
                $('#main_table').datagrid('load');
            }else {
                $alert(result.message);
            }
        },
        error:function () {
            console.log(url+" error")
        }
    });
}
//添加参数
function appendOption(url,$select,valueField,textField,all) {
    $select.combobox({
        url:url,
        valueField:valueField,
        textField:textField,
        loadFilter:function(result){
            if(result.code=='200'){
                result.data.unshift(all);
                return result.data;
            }
        }
    })
}